import {createRouter, createWebHashHistory} from 'vue-router'
import routerMap from './routerMap.js'
import request from "../axios/axios.js";


const Router = createRouter({
    history: createWebHashHistory(),
    routes: routerMap,
})

Router.beforeEach((to, from) => {
    if (to.path === '/admin') {
        if (!localStorage.getItem('token')) {
            return '/login'
        }
        request.get('/api/user/getCurrentUser').then(res => {
            if (res.data.code === 200) {
                return true
            } else {
                localStorage.removeItem('token')
                return '/login'

            }
        })
    }
    return true
})

export default Router

